package com.example.smartplay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.smartplay.entity.Result;
import com.example.smartplay.entity.Student;
import com.example.smartplay.mapper.StudentMapper;
import com.example.smartplay.service.StudentService;
import com.example.smartplay.utils.Md5Util;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2024-07-11
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>  implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    // 根据用户名查询
    @Override
    public Student findByUsername(String uesrname) {
      return studentMapper.findByUsername(uesrname);

    }
    // 注册
    @Override
    public void register(String username, String password) {
        // 密码加密
       String md5PwdStr = Md5Util.getMD5String(password);


        studentMapper.insert1(username, md5PwdStr);// 调用 insert 方法插入学生对象
    }

    //分页查询学生
    @Override
    public Result getstudent(Integer pageNum, Integer pageSize) {
        Page<Student> page = new Page<>(pageNum, pageSize);
        studentMapper.selectPage(page, null);
        return Result.success(page);
    }
    // 登录
    @Override
    public Result login(String username, String password) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", Md5Util.getMD5String(password));
        if (studentMapper.selectOne(queryWrapper) == null) {
            return Result.error("用户名或密码错误");
        }
         return Result.success(studentMapper.selectOne(queryWrapper));
        }
    // 根据邮箱查询
    @Override
    public Student findByEmail(String email) {
        return studentMapper.findByEmail(email);
    }

    //查询所有身份为1的学生
    @Override
    public List<Student> selectAllHeaders(Student student) {
        return studentMapper.selectAllHeaders(student);
    }



}



